package com.itheima.service.system.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.system.SysLogDao;
import com.itheima.domain.system.SysLog;
import com.itheima.factory.MapperFactory;
import com.itheima.service.system.SysLogService;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class SysLogServiceImpl implements SysLogService {

    @Override
    public void insert(SysLog sysLog) {
        SqlSession sqlSession = MapperFactory.getSqlSession();
        SysLogDao logDao = sqlSession.getMapper(SysLogDao.class);

        logDao.insert(sysLog);
        sqlSession.close();
    }

    @Override
    public PageInfo<SysLog> pageQuery(Integer pageNum, Integer pageSize) {
        SqlSession sqlSession = MapperFactory.getSqlSession();
        SysLogDao logDao = sqlSession.getMapper(SysLogDao.class);
        PageHelper.startPage(pageNum,pageSize);
        List<SysLog> list = logDao.findAll();
        PageInfo<SysLog> pageInfo = new PageInfo<>(list);
        sqlSession.close();
        return pageInfo;
    }

    @Override
    public List<SysLog> findAll() throws Exception {
        SqlSession sqlSession = MapperFactory.getSqlSession();
        SysLogDao logDao = sqlSession.getMapper(SysLogDao.class);
        List<SysLog> logList = logDao.findAll();
        System.out.println(logList);
        sqlSession.close();
        return logList;
    }

    public static void main(String[] args) throws Exception{
        List<SysLog> all = new SysLogServiceImpl().findAll();
        for (SysLog sysLog : all) {
            System.out.println(sysLog);

        }
    }

}
